Skip to content

fix(🌐): build the React reconciler on the first render, not at import - #4546

Open
wcandillon wants to merge 1 commit into
mainfrom
fix/lazy-reconciler-server-render
Open

wcandillon wants to merge 1 commit into
mainfrom
fix/lazy-reconciler-server-render

Conversation

@wcandillon

Copy link
Copy Markdown
Owner

Description

Fixes #4544

sksg/Reconciler.ts built its React reconciler when the module was evaluated. Building a reconciler hooks it into the React instance for good: createReconciler() wraps ReactSharedInternals.S around the hook already there. The Expo dev server evaluates the whole server bundle again for every web request, against one shared react, so every request left one more reconciler reachable from React, and the whole evaluated bundle behind it. The dev server ran out of memory after a dozen page loads.

The root cause is on the Expo side (expo/expo#54308); this is a mitigation on ours. Nothing changes for a client: the reconciler is built by the first render into a canvas instead of at import.

Agent-written content

Summary

  • The reconciler is built on first use (getReconciler()), and injectIntoDevTools() moves along with it.
  • SkiaSGRoot creates its React root in the first render(), not in its constructor. This part is needed on top of what the issue proposes: <Canvas> constructs its SkiaSGRoot in a useMemo, during render, and a <Canvas> does render on a server without CanvasKit. It renders into the root from a layout effect, which a server never runs.
  • unmount() on a root that never rendered resolves right away, without committing null into an empty root. <Canvas> never does that: its layout effect renders before its unmount effect exists.

Tests

  • renderer/__tests__/CanvasServerRender.spec.tsx: imports the library with the web modules and no CanvasKit, runs renderToString on a <Canvas>, and checks that ReactSharedInternals.S is the same before the import, after it, and after the render.
  • sksg/__tests__/LazyReconciler.spec.tsx: constructing and unmounting a root that never rendered leaves React untouched, the first render installs the hook, and later roots share the one reconciler.

The server render spec was also run against two other versions of Reconciler.ts to check that it tells them apart:

Reconciler.ts Result
This PR passes
main (reconciler built at import) fails right after the import
Lazy reconciler, root still created in the constructor (the issue's proposal) fails after the render

Local verification

  • yarn tsc, eslint on the changed files and the full yarn jest run in packages/skia pass (130 suites, 985 tests).
  • Not run: the reproduction from the issue against a real Expo dev server, and the device e2e suites, which run on CI.

Building a reconciler hooks it into the React instance for the lifetime of
the process: createReconciler() wraps ReactSharedInternals.S around the
hook already there. sksg/Reconciler.ts built one when the module was
evaluated, so a server that evaluates the bundle once per request against
a single React instance, as the Expo dev server does for web, kept one
reconciler per request, and the whole bundle behind it.

The reconciler is now built by the first render, along with its DevTools
registration. The React root of a SkiaSGRoot is created by the first
render as well, not by the constructor: a Canvas constructs its root while
it renders, which a server does too, and renders into it from a layout
effect, which a server never runs. Unmounting a root that never rendered
resolves without a commit.
wcandillon added a commit that referenced this pull request Nov 26, 2026
… (2.x backport of #4546)

Backport of #4546 to 2.x.

Building a reconciler hooks it into the React instance for the lifetime of
the process: createReconciler() wraps ReactSharedInternals.S around the
hook already there. sksg/Reconciler.ts built one when the module was
evaluated, so a server that evaluates the bundle once per request against
a single React instance, as the Expo dev server does for web, kept one
reconciler per request, and the whole bundle behind it.

The reconciler is now built by the first render, along with its DevTools
registration. The React root of a SkiaSGRoot is created by the first
render as well, not by the constructor: a Canvas constructs its root while
it renders, which a server does too, and renders into it from a layout
effect, which a server never runs. Unmounting a root that never rendered
resolves without a commit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sksg/Reconciler.js builds its reconciler at import, so every Expo dev server web render keeps its whole bundle alive

1 participant